package com.iqiyi.pps.epg.api.utils;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by karkarrotxu on 2017/3/13.
 */
public class ApiPassportUtil {
    public static final String SECRET_KEY = Configuration.getString("passport.secret.key");

    public static String doQuery(String url,Map<String, Object> headers,JSONObject params ){
        Map<String, String> map = new HashMap<>();
        map.put("source","epg");
        map.put("ptid","171");
        map.put("agenttype","171");
        map.put("timestamp",""+System.currentTimeMillis());
        if(params.containsKey("uid") ){
            map.put("uid",params.getString("uid"));
        }
        if(params.containsKey("nickname") ){
            map.put("nickname",params.getString("nickname"));
        }
        map.put("sign", sign(map, SECRET_KEY));
        String retValue = ApiHttpRequestUtil.postMethod(url , headers,  map );
        return retValue;
    }

    public static String sign(Map<String, String> params, String secretKey) {
        Map<String, String> sortedParams = new TreeMap<String, String>(params);
        sortedParams.remove("sign");
        StringBuilder sb = new StringBuilder();
        for (String key : sortedParams.keySet()) {
            String val = sortedParams.get(key);
            sb.append(key).append("=").append(StringUtils.defaultIfEmpty(val, "")).append("|");
        }
        return DigestUtils.md5Hex(sb.append(secretKey).toString());
    }
//    public static void main(String[] args){
//        testnickName();
//    }
//    public static void testnickName(){
//        String url ="http://passport.qiyi.domain/apis/inner/info/byNick.action";
//        String secretKey = "epglop6JD8ftf7BdPLQ2foALaxxf";
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("source","epg");
//        map.put("ptid","171");
//        map.put("agenttype","171");
//        map.put("nickname","刘德华");
//        map.put("timestamp",""+System.currentTimeMillis());
//        map.put("sign", sign(map, secretKey));
//        String retValue = ApiHttpRequestUtil.postMethod(url , null,  map );
//        System.out.print("testnickName retValue:"+retValue);
//    }
//    public static void testUid(){
//        String url ="http://passport.qiyi.domain/apis/inner/info/byUid.action";
//        String secretKey = "epglop6JD8ftf7BdPLQ2foALaxxf";
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("source","epg");
//        map.put("ptid","171");
//        map.put("agenttype","171");
//        map.put("uid","1010251465");
//        map.put("timestamp",""+System.currentTimeMillis());
//        map.put("sign", sign(map, secretKey));
//        String retValue = ApiHttpRequestUtil.postMethod(url , null,  map );
//        System.out.print(" testUid retValue:"+retValue);
//    }
}
